using System;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;

namespace PaulRz.Util
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class URLParser
	{
        private Match match = null;
        private string fullPath;

        public URLParser()
        {
        }
       
        public URLParser(string url)
		{
            Parse(url);
		}

        /// <summary>
        /// Parses given URL
        /// </summary>
        /// <param name="url"></param>
        public void Parse(string url)
        {
            Regex regex = new Regex(@"(^(?<proto>\w+)://)?(?<server>[^/]+?)(:(?<port>\d+))?(/(?<file>.+))*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            match = regex.Match(url);
            if (!match.Success)
                throw new ArgumentException("Passed URL has wrong format: " + url);
            fullPath = match.Result("${file}");

            regex = new Regex(@"(^(?<proto>\w+)://)?(?<server>[^/]+?)(:(?<port>\d+))?(/(?<file>.+?))*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            match = regex.Match(url);
            if (!match.Success)
                throw new ArgumentException("Passed URL has wrong format: " + url);
        }

        /// <summary>
        /// Return protocol part of URL
        /// </summary>
        public string Protocol
        {
            get
            {
                return (match.Success) ? match.Result("${proto}") : String.Empty;
            }
        }
        /// <summary>
        /// Returns server part of URL
        /// </summary>
        public string Server
		{
            get
            {
                return (match.Success) ? match.Result("${server}") : String.Empty;
            }
        }
        /// <summary>
        /// Returns port part of URL
        /// </summary>
        public int Port
        {
            get
            {
                try
                {
                    return (match.Success) ? Convert.ToUInt16(match.Result("${port}")) : -1;
                }
                catch (Exception e) 
                {
                    return 0;
                }
            }
        }
        /// <summary>
        /// Returns short file name. For example, if url is ftp://host/folder/file.pdf property returns file.pdf
        /// </summary>
        public string File
        {
            get
            {
                return (match.Success) ? match.Result("${file}") : String.Empty;
            }
        }

        /// <summary>
        /// Returns full path to file. For example, if url is ftp://host/folder/file.pdf property returns folder/file.pdf
        /// </summary>
        public string FullPath 
        {
            get 
            {
                return fullPath;
            }
        }

        /// <summary>
        /// Returns directory. For example, if url is ftp://host/folder/file.pdf property returns folder
        /// </summary>
        public string Directory
        {
            get
            {
                if (FullPath == File) return String.Empty;
                return (match.Success) ? FullPath.Substring(0, FullPath.Length - File.Length - 1) : String.Empty;
            }
        }
    }
}

	

